﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PaperSystem.Models;

namespace PaperSystem.Models
{
    public class PublicRepository
    {
        PaperDatabaseEntities db;
        public PublicRepository()
        {
            db = new PaperDatabaseEntities();
        }

        //根据用户id和角色返回用户名字
        public string UserName(string userId, string role)
        {
            string username = "";
            try
            {
                if (role.Equals("管理员"))
                {
                    int user = Convert.ToInt32(userId);
                    var tempdata = db.Administrator.Where(a => a.Aid == user).FirstOrDefault();
                    username = tempdata.Aname;
                }
                else if (role.Equals("教师"))
                {
                    var tempdata = db.Teacher.Where(t => t.Tid == userId).FirstOrDefault();
                    username = tempdata.Tname;
                }
                else if (role.Equals("学生"))
                {
                    var tempdata = db.Student.Where(s => s.Sid == userId).FirstOrDefault();
                    username = tempdata.Sname;
                }
            }
            catch (Exception e)
            {
                return username;
            }
            return username;
        }

        //返回所有课程名称下拉列表
        public SelectList GetCourseDropdownList()
        {
            var list = db.Course.ToList();
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var a in list)
            {
                listItem.Add(new SelectListItem { Text = a.Cname, Value = a.Cname });
            }
            return new SelectList(listItem, "Value", "Text", "");
        }

        //创建题目类型下拉框的内容
        public SelectList GetTypeDropdownList()
        {
            //TODO：内容待定
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "阅读理解", Value = "阅读理解" });
            listItem.Add(new SelectListItem { Text = "完形填空", Value = "完形填空" });
            listItem.Add(new SelectListItem { Text = "翻译", Value = "翻译" });
            listItem.Add(new SelectListItem { Text = "作文", Value = "作文" });
            listItem.Add(new SelectListItem { Text = "综合选择", Value = "综合选择" });
            SelectList list = new SelectList(listItem, "Value", "Text");
            return list;
        }

        //创建试卷用途类型下拉框的内容
        public SelectList GetTorEDropdownList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "考试", Value = "考试" });
            listItem.Add(new SelectListItem { Text = "练习", Value = "练习" });
            SelectList list = new SelectList(listItem, "Value", "Text");
            return list;
        }

        //创建是否批改过的下拉框内容
        public SelectList GetCorrectedDropdownList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "未批改", Value = "未批改" });
            listItem.Add(new SelectListItem { Text = "已批改", Value = "已批改" });
            SelectList list = new SelectList(listItem, "Value", "Text");
            return list;
        }

        //根据信息获取题目显示列表
        public List<QuestionList> GetQuestionList(QuestionQuery info)
        {
            var list = (from a in db.Ex_question
                        select new QuestionList()
                        {
                            chapter = a.Chapter,
                            content = a.Econtent,
                            course = a.Course.Cname,
                            creator = a.Teacher.Tname,
                            eid = a.Eid,
                            knowledge = a.Knowledge,
                            time = a.Etime,
                            TorE = a.TorE,
                            type = a.Etype,
                        });
            if (info != null)
            {
                if (info.course != null)
                {
                    list = list.Where(a => a.course == info.course);
                }
                if (info.type != null)
                {
                    list = list.Where(a => a.type == info.type);
                }
            }
            return list.ToList();
        }

        //根据信息获取题目显示列表(添加试卷页面用到)
        public List<QuestionList> GetQuestionList(string course, string type, string TorE, string chapter, string knowledge, string creator)
        {
            var list = (from a in db.Ex_question
                        select new QuestionList()
                        {
                            chapter = a.Chapter,
                            content = a.Econtent,
                            course = a.Course.Cname,
                            creator = a.Teacher.Tname,
                            eid = a.Eid,
                            knowledge = a.Knowledge,
                            time = a.Etime,
                            TorE = a.TorE,
                            type = a.Etype,
                        });
            if (!course.Equals(""))
            {
                list = list.Where(a => a.course == course);
            }
            if (!type.Equals(""))
            {
                list = list.Where(a => a.type == type);
            }
            if (!TorE.Equals(""))
            {
                list = list.Where(a => a.TorE == TorE);
            }
            if (!chapter.Equals(""))
            {
                list = list.Where(a => a.chapter == chapter);
            }
            if (!knowledge.Equals(""))
            {
                list = list.Where(a => a.knowledge == knowledge);
            }
            if (!creator.Equals(""))
            {
                list = list.Where(a => a.creator == creator);
            }
            return list.ToList();
        }

        //获取试卷基本信息
        public Paper GetPaperInfo(int pid)
        {
            try 
            {
                Paper paper=new Paper();
                paper = db.Paper.Where(a => a.Pid == pid).FirstOrDefault();
                return paper;
            }
            catch(Exception e)
            {
                return new Paper();
            }
        }
    }
}
